/*
 * @Author: yaorui.moye
 * @Date: 2020-08-27 12:17:06
 * @LastEditors: yaorui.moye
 * @LastEditTime: 2020-08-27 22:11:49
 * @Description: file content
 * @FilePath: /my-tools-java/date-utils/src/main/java/DateUtils.java
 */

import java.time.chrono.IsoChronology;
import java.util.Date;

public class DateUtils {
  public static final int YEAR_DAY = 365;
  public static final int LEAP_YEAR_DAY = 366;
  /**
   * relatively time millis
   */
  public static final long SECOND = 1000;
  public static final long MINUTE = SECOND * 60;
  public static final long HOUR = MINUTE * 60;
  public static final long DAY = HOUR * 24;
  public static final long WEEK = DAY * 7;
  public static final long MONTH = DAY * 30;
  public static final long YEAR = YEAR_DAY * DAY;

  /**
   * @param year year
   * @return day num
   * @description: get the day number of year
   */
  public static long getDaysForYear(int year) {
    if (IsoChronology.INSTANCE.isLeapYear(year)) {
      return LEAP_YEAR_DAY;
    }
    return YEAR_DAY;
  }

  /**
   * @param year year
   * @return {long}
   * @description: get second of year
   */
  public static long getSecondForYear(int year) {
    return getDaysForYear(year) * DAY;
  }

  /**
   * @param year year
   * @return {long}
   * @description: get second of year
   */
  public static long getMinuteForYear(int year) {
    return getSecondForYear(year) / MINUTE;
  }

  /**
   * @param year year
   * @return {long}
   * @description: get second of year
   */
  public static long getHourForYear(int year) {
    return getSecondForYear(year) / HOUR;
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {float}
   * @description: Get the interval Day between two dates
   */
  public static float getYearBetweenTwoDate(long earlyDate, long laterDate) {
    long secondBetweenTwoDate = getSecondBetweenTwoDate(earlyDate, laterDate);
    return Math.round((secondBetweenTwoDate / (float) YEAR) * 10) * 0.1f;
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {float}
   * @description: Get the interval Day between two dates
   */
  public static float getYearBetweenTwoDate(Date earlyDate, Date laterDate) {
    return getYearBetweenTwoDate(earlyDate.getTime(), laterDate.getTime());
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {float}
   * @description: Get the interval Day between two dates
   */
  public static float getMonthBetweenTwoDate(long earlyDate, long laterDate) {
    long secondBetweenTwoDate = getSecondBetweenTwoDate(earlyDate, laterDate);
    return Math.round((secondBetweenTwoDate / (float) MONTH) * 10) * 0.1f;
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {float}
   * @description: Get the interval Day between two dates
   */
  public static float getMonthBetweenTwoDate(Date earlyDate, Date laterDate) {
    return getMonthBetweenTwoDate(earlyDate.getTime(), laterDate.getTime());
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {float}
   * @description: Get the interval Day between two dates
   */
  public static float getWeekBetweenTwoDate(long earlyDate, long laterDate) {
    long secondBetweenTwoDate = getSecondBetweenTwoDate(earlyDate, laterDate);
    return Math.round((secondBetweenTwoDate / (float) WEEK) * 10) * 0.1f;
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {float}
   * @description: Get the interval Day between two dates
   */
  public static float getWeekBetweenTwoDate(Date earlyDate, Date laterDate) {
    return getWeekBetweenTwoDate(earlyDate.getTime(), laterDate.getTime());
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {float}
   * @description: Get the interval Day between two dates
   */
  public static float getDayBetweenTwoDate(long earlyDate, long laterDate) {
    long secondBetweenTwoDate = getSecondBetweenTwoDate(earlyDate, laterDate);
    return Math.round((secondBetweenTwoDate / (float) DAY) * 10) * 0.1f;
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {float}
   * @description: Get the interval Day between two dates
   */
  public static float getDayBetweenTwoDate(Date earlyDate, Date laterDate) {
    return getDayBetweenTwoDate(earlyDate.getTime(), laterDate.getTime());
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {float}
   * @description: Get the interval hours between two dates
   */
  public static float getHoursBetweenTwoDate(long earlyDate, long laterDate) {
    long secondBetweenTwoDate = getSecondBetweenTwoDate(earlyDate, laterDate);
    return Math.round((secondBetweenTwoDate / (float) HOUR) * 10) * 0.1f;
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {float}
   * @description: Get the interval hours between two dates
   */
  public static float getHoursBetweenTwoDate(Date earlyDate, Date laterDate) {
    return getHoursBetweenTwoDate(earlyDate.getTime(), laterDate.getTime());
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {long}
   * @description: Get the interval second between two dates
   */
  public static long getMinuteBetweenTwoDate(long earlyDate, long laterDate) {
    long secondBetweenTwoDate = getSecondBetweenTwoDate(earlyDate, laterDate);
    return Math.round(secondBetweenTwoDate / (float) MINUTE);
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {double}
   * @description: Get the interval second between two dates
   */
  public static double getMinuteBetweenTwoDate(Date earlyDate, Date laterDate) {
    return getMinuteBetweenTwoDate(earlyDate.getTime(), laterDate.getTime());
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {long}
   * @description: Get the interval second between two dates
   */
  public static long getSecondBetweenTwoDate(long earlyDate, long laterDate) {
    long betweenToDateTimeMillis = getBetweenTwoDateTimeMillis(earlyDate, laterDate);
    return betweenToDateTimeMillis / 1000;
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {long}
   * @description: Get the interval second between two dates
   */
  public static long getSecondBetweenTwoDate(Date earlyDate, Date laterDate) {
    return getSecondBetweenTwoDate(earlyDate.getTime(), laterDate.getTime());
  }

  /**
   * @param earlyDate Timestamp
   * @param laterDate Timestamp
   * @return {long}
   * @description: Get the interval timeMillis between two dates
   */
  public static long getBetweenTwoDateTimeMillis(long earlyDate, long laterDate) {
    return laterDate - earlyDate;
  }

  /**
   * @param earlyDate earlyDate
   * @param laterDate laterDate
   * @return {long}
   * @description: Get the interval timeMillis between two dates
   */
  public static long getBetweenTwoDateTimeMillis(Date earlyDate, Date laterDate) {
    return laterDate.getTime() - earlyDate.getTime();
  }
}
